﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;
using System.Globalization;
using System.IO;
using System.ServiceModel;

namespace _0512301_0512430.Main
{
    class MapSlate
    {
        private double _longitude;

        public double Longitude
        {
            get { return _longitude; }
            set { _longitude = value; }
        }
        private double _latitude;

        public double Latitude
        {
            get { return _latitude; }
            set { _latitude = value; }
        }
        private int _zoom;

        public int Zoom
        {
            get { return _zoom; }
            set { _zoom = value; }
        }

        
        public MapSlate()
        {
            LatLng latlng = this.GetLatLng("HO CHI MINH");

            _longitude = latlng.Longitude;
            _latitude = latlng.Latitude;

            _zoom = 13;
            _mapType = "roadmap";
        }

        private string _mapType;

        public string MapType
        {
            get { return _mapType; }
            set { _mapType = value; }
        }

        
        public LatLng GetLatLng(String center)
        {
            string url = "http://maps.google.com/maps/api/staticmap?sensor=false";
            GoogleMaps gmap = new GoogleMaps("ABQIAAAArERuK-DUei4Mr7gxC1b53BRgAo7oAi_Gt50xUDW7e6DgrBynPRQbLXKy73TID0ljDRtjA1CGdqZQ3w");
            LatLng latlng = gmap.GetLatLng(url);
            return latlng;
        }
        public static Bitmap GetMapImage(double lat, double lng, string mapType, int zoom, int size)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom={2}&size={3}x{3}&maptype="+ mapType +"&sensor=true_or_false", lat.ToString("f6", new CultureInfo("en-us")), lng.ToString("f6", new CultureInfo("en-us")), zoom, size);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            return new Bitmap(response.GetResponseStream());
        }
        public static Bitmap GetMapImage()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("http://maps.google.com/maps/api/staticmap?center=ho+chi+minh&zoom=13&size=214x214&maptype=roadmap&sensor=false");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            return new Bitmap(response.GetResponseStream());
        }
        public static Bitmap GetCacheImage(double lat, double lng, int zoom, int size)
        {
            return new Bitmap(1, 1);
        }

        public double[] LocationGeoCode(double lat, double lng, string mapType, int zoom, int size)
        {
            string csvValues = "";

            double[] result = new double[3];

            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom={2}&size={3}x{3}&maptype=" + mapType + "&sensor=true", lat.ToString("f6", new CultureInfo("en-us")), lng.ToString("f6", new CultureInfo("en-us")), zoom, size);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

               

                Stream objWebStream = response.GetResponseStream();

                using (StreamReader objStreamReader = new StreamReader(objWebStream))
                {
                    csvValues = objStreamReader.ReadToEnd();
                }

                if (!(string.IsNullOrEmpty(csvValues)))
                {
                    string[] geoValues = csvValues.Split(new char[] { ',' });

                    if (geoValues.Length > 0)
                    {
                        double Latitude = Convert.ToDouble(geoValues[2].ToString());
                        double Longitude = Convert.ToDouble(geoValues[3].ToString());
                        result[0] = Latitude;
                        result[1] = Longitude;
                        result[2] = 3.4;
                    }
                }
            }
            catch
            {

            }
            return result;
        }
        
    }
}
